﻿// -----------------------------------------------------------------------
// <copyright file="PhieuKhamBenhChiTietDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class PhieuKhamBenhChiTietDataAccess
    {
        public bool InsertPhieuKhamBenhChiTiet(PhieuKhamBenhChiTietEntity obj)
        {
            try
            {
                string sql = "spPhieuKhamBenhChiTiet_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhamBenh", DbType.String, obj.MaKhamBenh);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, obj.MaChiTieu);
                db.AddInParameter(cmd, "DonGia", DbType.Double, obj.DonGia);
                db.AddInParameter(cmd, "DaThuTien", DbType.Int32, obj.DaThuTien);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertPhieuKhamBenhChiTiet", ex.Message); throw ex; }
        }

        public bool EditPhieuKhamBenhChiTiet(PhieuKhamBenhChiTietEntity obj)
        {
            try
            {
                string sql = "spPhieuKhamBenhChiTiet_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhamBenh", DbType.String, obj.MaKhamBenh);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, obj.MaChiTieu);
                db.AddInParameter(cmd, "DonGia", DbType.Double, obj.DonGia);
                db.AddInParameter(cmd, "DaThuTien", DbType.Int32, obj.DaThuTien);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdatePhieuKhamBenhChiTiet", ex.Message); throw ex; }
        }

        public bool DeletePhieuKhamBenhChiTietByMaKhamBenh(string maKhamBenh)
        {
            try
            {
                string sql = "spPhieuKhamBenhChiTiet_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhamBenh", DbType.String, maKhamBenh);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeletePhieuKhamBenhChiTietByMaKhamBenh", ex.Message); throw ex; }
        }

        public PhieuKhamBenhChiTietEntity GetPhieuKhamBenhChiTiet(string maKhamBenh,string maChiTieu)
        {
            try
            {
                PhieuKhamBenhChiTietEntity obj = null;
                string sql = "spPhieuKhamBenhChiTiet_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhamBenh", DbType.String, maKhamBenh);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, maChiTieu);

                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuKhamBenhChiTietEntity();
                        obj.MaKhamBenh = reader["MaKhamBenh"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuKhamBenhChiTiet", ex.Message); throw ex; }
        }

        public List<PhieuKhamBenhChiTietEntity> GetPhieuKhamBenhChiTietList()
        {
            try
            {
                List<PhieuKhamBenhChiTietEntity> list = new List<PhieuKhamBenhChiTietEntity>();
                PhieuKhamBenhChiTietEntity obj = null;
                string sql = "spPhieuKhamBenhChiTiet_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuKhamBenhChiTietEntity();
                        obj.MaKhamBenh = reader["MaKhamBenh"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuKhamBenhChiTietList", ex.Message); throw ex; }
        }

    }
}